package eworldpacs.ui;

import com.eworld.pacsviewer.R;

import leadtools.dicom.DicomTag;
import leadtools.dicom.DicomTagTable;
import leadtools.dicom.DicomUid;
import leadtools.dicom.DicomUidTable;
import leadtools.dicom.DicomVr;
import leadtools.dicom.DicomVrTable;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TablesViewActivity extends Activity {
   private static final String TAG_TABLE_INDEX = "tag_table_index";
   private static final String TAG_TABLE_OFFSET = "tag_table_offset";
   
   private static final String VR_TABLE_INDEX = "vr_table_index";
   private static final String VR_TABLE_OFFSET = "vr_table_offset";
   
   private static final String UID_TABLE_INDEX  = "uid_table_index";
   private static final String UID_TABLE_OFFSET = "uid_table_offset";
   private static final String CURRENT_TAB = "current_tab";
   
   ListView _tagTableListView;
   ListView _vrTableListView;
   ListView _uidTableListView;
   TabHost _tabHost;

   @SuppressWarnings("deprecation")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
                    
        setContentView(R.layout.tables_view);
        
        Display display = getWindowManager().getDefaultDisplay();
        int windowWidth = 0;
        if(Build.VERSION.SDK_INT < 13) {
           windowWidth = display.getWidth();
        }
        else {
           Point size = new Point();
           display.getSize(size);
           windowWidth = size.x;
        }
        
        _tagTableListView = new ListView(this);
        _tagTableListView.setCacheColorHint(Color.DKGRAY);
       double[] tagTableColumnWidthRatios = new double[]{0.4,0.2,0.15,0.07,0.07,0.07};
       String[] tagTableColumnTitles = new String[]{"Name", "Code", "Mask", "Min\nVM", "Max\nVM", "Div\nVM"};
       DicomTableAdapter tagTableAdapter = DicomTableAdapter.Create(this, 
                                                     android.R.layout.simple_list_item_1,
                                                     windowWidth,
                                                     DicomTagTable.getInstance().getCount(),
                                                     6,
                                                     tagTableColumnWidthRatios,
                                                     tagTableColumnTitles
                                                     );
       tagTableAdapter.setTextRetriever(new DicomTableTextRetriever(){

         @Override
         public String[] getStringsForRow(int row) {
            String[] rowTextValues = new String[6];
            DicomTag tag = DicomTagTable.getInstance().findByIndex(row);
            rowTextValues[0] = tag.getName();
            rowTextValues[1] = DicomDemoUtils.ElementToString(tag.getCode());
            rowTextValues[2] = Integer.toHexString((int)tag.getMask());
            rowTextValues[3] = Integer.toString((int)tag.getMinVm());
            rowTextValues[4] = Integer.toString((int)tag.getMaxVm());
            rowTextValues[5] = Integer.toString((int)tag.getDivider());
            return rowTextValues;
         }
       });
       _tagTableListView.addHeaderView(tagTableAdapter.getCustomHeaderView());
       _tagTableListView.setAdapter(tagTableAdapter);
       
       
       _vrTableListView = new ListView(this);
       _vrTableListView.setCacheColorHint(Color.DKGRAY);
       double[] vrTableColumnWidthRatios = new double[]{0.08,0.3,0.17,0.1,0.35};
       String[] vrTableColumnTitles = new String[]{"Code", "Name", "Length", "Unit", "Restriction"};
       DicomTableAdapter vrTableAdapter = DicomTableAdapter.Create(this, 
                                                     android.R.layout.simple_list_item_1,
                                                     windowWidth,
                                                     DicomVrTable.getInstance().getCount(),
                                                     5,
                                                     vrTableColumnWidthRatios,
                                                     vrTableColumnTitles
                                                     );
       vrTableAdapter.setTextRetriever(new DicomTableTextRetriever(){

         @Override
         public String[] getStringsForRow(int row) {
            String[] rowTextValues = new String[5];
            DicomVr vr = DicomVrTable.getInstance().findByIndex(row);
            rowTextValues[0] = vr.getCode().toString();
            rowTextValues[1] = vr.getName();
            rowTextValues[2] = Integer.toString(vr.getLength());
            rowTextValues[3] = Integer.toString(vr.getUnitSize());
            rowTextValues[4] = vr.getRestriction().toString();
            return rowTextValues;
         }
       });
       _vrTableListView.addHeaderView(vrTableAdapter.getCustomHeaderView());
       _vrTableListView.setAdapter(vrTableAdapter);
       
       _uidTableListView = new ListView(this);
       _uidTableListView.setCacheColorHint(Color.DKGRAY);
       double[] uidTableColumnWidthRatios = new double[]{0.35,0.4,0.25};
       String[] uidTableColumnTitles = new String[]{"Code", "Name", "Type"};
       DicomTableAdapter uidTableAdapter = DicomTableAdapter.Create(this, 
                                                     android.R.layout.simple_list_item_1,
                                                     windowWidth,
                                                     DicomUidTable.getInstance().getCount(),
                                                     3,
                                                     uidTableColumnWidthRatios,
                                                     uidTableColumnTitles
                                                     );
       uidTableAdapter.setTextRetriever(new DicomTableTextRetriever(){

         @Override
         public String[] getStringsForRow(int row) {
            String[] rowTextValues = new String[3];
            DicomUid uid = DicomUidTable.getInstance().findByIndex(row);
            rowTextValues[0] = uid.getCode().toString();
            rowTextValues[1] = uid.getName();
            rowTextValues[2] = uid.getType().toString();
            return rowTextValues;
         }
       });
       _uidTableListView.addHeaderView(uidTableAdapter.getCustomHeaderView());
       _uidTableListView.setAdapter(uidTableAdapter);
       
       
       _tabHost = (TabHost)findViewById(android.R.id.tabhost);
       _tabHost.setup();
       
       TabSpec tagTableTabSpec = _tabHost.newTabSpec("tagtable");
       TabViewFactory tagTableTabViewFactory = new TabViewFactory(_tagTableListView);
       tagTableTabSpec.setIndicator("Tag Table").setContent(tagTableTabViewFactory);
       
       TabSpec vrTableTabSpec = _tabHost.newTabSpec("tagtable");
       TabViewFactory vrTableTabViewFactory = new TabViewFactory(_vrTableListView);
       vrTableTabSpec.setIndicator("VR Table").setContent(vrTableTabViewFactory);	
       
       TabSpec uidTableTabSpec = _tabHost.newTabSpec("tagtable");
       TabViewFactory uidTableTabViewFactory = new TabViewFactory(_uidTableListView);
       uidTableTabSpec.setIndicator("UID Table").setContent(uidTableTabViewFactory);	
       
       
       
       _tabHost.addTab(tagTableTabSpec);
       _tabHost.addTab(vrTableTabSpec);
       _tabHost.addTab(uidTableTabSpec);
       
       if (savedInstanceState != null){
          int tagTableIndex = savedInstanceState.getInt(TAG_TABLE_INDEX);
          int tagTableOffset = savedInstanceState.getInt(TAG_TABLE_OFFSET);
          int vrTableIndex = savedInstanceState.getInt(VR_TABLE_INDEX);
          int vrTableOffset = savedInstanceState.getInt(VR_TABLE_OFFSET);
          int uidTableIndex = savedInstanceState.getInt(UID_TABLE_INDEX);
          int uidTableOffset = savedInstanceState.getInt(UID_TABLE_OFFSET);
          int currentTab = savedInstanceState.getInt(CURRENT_TAB);
          _tagTableListView.setSelectionFromTop(tagTableIndex, tagTableOffset);
          _vrTableListView.setSelectionFromTop(vrTableIndex, vrTableOffset);
          _uidTableListView.setSelectionFromTop(uidTableIndex, uidTableOffset);
          _tabHost.setCurrentTab(currentTab);
       }
   }
   
   @Override
   public void onSaveInstanceState(Bundle savedInstanceState) {
       // Save the user's current game state
      View v = null;
      int tagTableIndex = _tagTableListView.getFirstVisiblePosition();
      v = _tagTableListView.getChildAt(0);
      int tagTableTop = (v == null) ? 0 : v.getTop();
      
      int vrTableIndex = _vrTableListView.getFirstVisiblePosition();
      v = _vrTableListView.getChildAt(0);
      int vrTableTop = (v == null) ? 0 : v.getTop();
      
      int uidTableIndex = _uidTableListView.getFirstVisiblePosition();
      v = _uidTableListView.getChildAt(0);
      int uidTableTop = (v == null) ? 0 : v.getTop();
      
      
      
       savedInstanceState.putInt(TAG_TABLE_INDEX, tagTableIndex);
       savedInstanceState.putInt(TAG_TABLE_OFFSET, tagTableTop);
       
       savedInstanceState.putInt(VR_TABLE_INDEX, vrTableIndex);
       savedInstanceState.putInt(VR_TABLE_OFFSET, vrTableTop);
       
       savedInstanceState.putInt(UID_TABLE_INDEX, uidTableIndex);	    
       savedInstanceState.putInt(UID_TABLE_OFFSET, uidTableTop);
       
       savedInstanceState.putInt(CURRENT_TAB, _tabHost.getCurrentTab());
       
       // Always call the superclass so it can save the view hierarchy state
       super.onSaveInstanceState(savedInstanceState);
   }
}
